#!/usr/bin/perl
#################################################################################
#  Copyright (C) 2007 [F.G. Hattingh]											
#																				
#	 This program is free software; you can redistribute it and/or modify		
#	it under the terms of the GNU General Public License as published by		
#	the Free Software Foundation; either version 2 of the License, or			
#	(at your option) any later version.											
#																				
#	This program is distributed in the hope that it will be useful,				
#	but WITHOUT ANY WARRANTY; without even the implied warranty of				
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the				
#	GNU General Public License for more details.								
#																				
#	You should have received a copy of the GNU General Public License			
#	along with this program; if not, write to the Free Software					
#	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA		
#																			
#	Contact me at: hattingh.fredre@gmail.com						
#
# An example program to show how mixp.pm works
#################################################################################

use Mixip::Main; #Use the Mixip lib

#Create a new Mixip::Main object
$mxit = Main->new();



#Turn on DEBUG to see more information
$mxit->debug(1);

#Setup my callback functions
$mxit->add_handler('raw_line',\&raw_line);  #When a raw line is recieved from server
$mxit->add_handler('end_read',\&end_read); #When the loop that read data from the server comes to an end
$mxit->add_handler('connecting',\&connecting); #When the connection is atempting
$mxit->add_handler('start_read',\&start_read); #When the socket is reading from server
$mxit->add_handler('connected',\&connected); #When we are connected to server 
$mxit->add_handler('connect_fail',\&connect_fail); #OOps the connection failed to connect
$mxit->add_handler('raw_send',\&raw_send); #You just sent a raw message to the server 
$mxit->add_handler('login_send',\&login_send); #You just sent a login to the server 
$mxit->add_handler('contacts_received',\&contacts_received); #New list of contacts recived
$mxit->add_handler('contact_status_change',\&contact_status_change); #One of the contacts changed status 
$mxit->add_handler('message_received',\&message_received); #A new message was sent to us
$mxit->add_handler('contact_invite_received',\&contact_invite_received); #We recived a contact invite


#Setup the user details



$mxit->number("27834151623"); #Make sure about the 27
$mxit->pin("1985");
$mxit->cid("24363097C4-150B-4693-840C-87C012EFC891279BE29C");

#Do connect
#$mxit->connect;

#Do login
#$mxit->login;
#$mxit->test;

##Get the cid from a jad file
#$mxit->get_jad_cid("/home/fredre/Desktop/12192010492E70BU.jad");

sub contact_invite_received
{
my $invite = @_[4]; #Argument 4 the hash 
print "Invite received from $invite->{'nick'} ($invite->{'host'}) ($invite->{'mes'})\n";
}

sub message_received
{

#Display the new message to user
my $host_name = @_[5]; 
my $mes = @_[6];

#A message only sends the host name so get the nick so we can know from who the message is:
my $nick = $mxit->get_nick($host_name);

print "$nick [$host_name] : $mes\n";

}

sub contacts_received
{

&out_t("You have the following contacts:");

my $name_h = @_[4]; #Argument 4 the hash contacts 


for $contact_h (@$name_h )
{
	 print "$contact_h->{ 'nick' }\n";
}

}

sub contact_status_change
{

my $name_h = @_[4]; #Argument 4 the hash contacts that changed 

for $contact_h (@$name_h )
{   
	#Tell user if Status changed
	if(%$contact_h->{'num1'} eq 0)
	{
	print "$contact_h->{ 'nick' } is now offline\n";
	}
	
	if(%$contact_h->{'num1'} eq 1)
	{
	print "$contact_h->{ 'nick' } is now online\n";
	}
	
	if(%$contact_h->{'num1'} eq 2)
	{
	print "$contact_h->{ 'nick' } is now away\n";
	}
	
	if(%$contact_h->{'num1'} eq 4)
	{
	print "$contact_h->{ 'nick' } is now busy\n";
	}
	
	#Tell user if Mood changed
	if(%$contact_h->{'num3'} eq 4)
	{
	print "$contact_h->{ 'nick' } is now Happy :) \n";
	}
	
	if(%$contact_h->{'num3'} eq 1)
	{
	print "$contact_h->{ 'nick' } is now Angry (T_T) \n";
	}
	
	if(%$contact_h->{'num3'} eq 9)
	{
	print "$contact_h->{ 'nick' } is now Sick :-~) \n";
	}
	#You get my drift...
}


}

#Read all the raw lines from server and print them
sub raw_line 
{
my $line = @_[1];
#&out_t("RAW SERV -> $line");
}

sub login_send
{
&out_t("Doing login");
}

#Notify user that the server stoped reading
sub end_read
{
&out_t("SERVER stoped reading !!!!");
#print "Going to try and reconect\n";
#$mxit->connect; Reconect if you like
} 

sub connecting
{
&out_t("Connecting...");
}

sub start_read
{
&out_t("Starting to read from socket...");
}

sub connected
{
&out_t("CONNECTED !!!");
}

sub connect_fail
{
&out_t("Connection failed !!!!");
}

sub raw_send
{
&out_t("You sent a raw message to the server !!!!");


}

#Output must go to the curses textview
sub out_t
{
my $message = @_[0];
print "$message\n";
}

#Read user input and take correct action 
#Also makes sure we dont run of the botom

while(<STDIN>)
{
my $new_text = $_;
chomp($new_text);

#Toest Toets
if($new_text eq '/test')
{
&out_t("Yes test is good young Skywalker");
$mxit->test();
}

#Connect to server
if($new_text eq '/connect')
{
$mxit->connect();
$mxit->login();
}

#Leave (koos will be sad)
if($new_text eq '/logout')
{
$mxit->logout;
}

##Leave
if($new_text eq '/disconnect')
{
$mxit->disconnect;
}

#Send a raw message if you go rambo and build the packet yourself
if($new_text =~ m/^\/raw (.*)/)
{
$mxit->send_raw($1);
}

#Delete a contact
if($new_text =~ m/^\/del (.*)/) 
{
print "Removing contact named: $1 \n";
$mxit->delete_contact($1);
}

#Reject a contact
if($new_text =~ m/^\/reject (.*)/)  
{
print "Rejecting contact named: $1 \n";
$mxit->reject_contact_inv($1); 
} 

#Accept a contact
if($new_text =~ m/^\/accept (.*)/)  
{
print "Accepting contact named: $1 \n";
$mxit->accept_contact_inv($1); 
} 



#Send a message to someone
if($new_text =~ m/^\/msg (\w+) (.*)/)  
{
$mxit->send_message($1,$2);
}

#Invite a contact 
if($new_text =~ m/^\/invite (\w+) (.*)/)  
{
print "Inviting $1 at number $2\n";
$mxit->invite_contact($2,$1);
}

}


